library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
use WORK.COMMON.ALL;

entity Judge is
port (
	CLK_MAIN, CLK_11, CLK_25, CLK_100, RST: in std_logic;
	R_KEY, L_ARROW, R_ARROW: in std_logic;
	NOW_PLAYER: in std_logic;
	NOW_CUT: in std_logic;
	NOW_TRUNKS: in ITrunkList;
	NOW_HP: in std_logic_vector(9 downto 0);
	NOW_SCORE: in std_logic_vector(9 downto 0);
	NOW_TIMER: in std_logic_vector(31 downto 0);
	TRUNK_E: out std_logic; -- 生成树干使能
	JUDGE_E: out std_logic; -- 是否可以进行下一个命令
	CMD: out std_logic_vector(3 downto 0) -- 命令
);
end Judge;

architecture Judge_bhv of Judge is
begin
	process(R_KEY, L_ARROW, R_ARROW, NOW_HP, NOW_CUT, NOW_TIMER)
	begin
		if NOW_HP=HP_ZERO OR (NOW_CUT = PLAYER_CUT_NONE AND NOW_TIMER > TIMER_INTERVAL AND HP_ZERO <= NOW_HP AND NOW_HP <= HP_MAXN) then
			JUDGE_E <= '1';
			if R_KEY='1' then
				CMD <= CMD_RESTART;
				TRUNK_E <= '0';
			elsif NOW_HP=HP_ZERO then
				CMD <= CMD_KEEP;
				TRUNK_E <= '0';
			elsif L_ARROW='1' then
				CMD <= CMD_PLAYER_LEFT;
				if NOW_TRUNKS(0) = TRUNK_DL OR NOW_TRUNKS(0) = TRUNK_DR then
					TRUNK_E <= '0';
				else
					TRUNK_E <= '1';
				end if;
			elsif R_ARROW='1' then
				CMD <= CMD_PLAYER_RIGHT;
				if NOW_TRUNKS(0) = TRUNK_DL OR NOW_TRUNKS(0) = TRUNK_DR then
					TRUNK_E <= '0';
				else
					TRUNK_E <= '1';
				end if;
			else
				CMD <= CMD_NONE;
				TRUNK_E <= '0';
			end if;
		else
			JUDGE_E <= '0';
			CMD <= CMD_NONE;
			if HP_ZERO <= NOW_HP AND NOW_HP <= HP_MAXN then
				TRUNK_E <= '0';
			else
				TRUNK_E <= '1';
			end if;
		end if;
	end process;
end Judge_bhv;
